﻿using ManagoMes.Read.API.Applications.SellStockSearchRead;
using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace ManagoMes.Read.API.Applications.ProductionRead
{
    public class ProductionBackfillHandler : IRequestHandler<ProductionBackfillCommand, ResultsDTO<ProductionDTO>>
    {
        private readonly IBateRepository<ProductionDTO> _productionRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionRepository"></param>
        public ProductionBackfillHandler(IBateRepository<ProductionDTO> productionRepository)
        {
            _productionRepository = productionRepository;
        }
        /// <summary>
        /// 生产报工回填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO<ProductionDTO>> Handle(ProductionBackfillCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapper = new DapperHelper();
            var conn = dapper.MangoMesRead;
            var list = new ResultsDTO<ProductionDTO>() { Code = 0 };
            var str = "select * from productionwork inner join productionworkorders " +
            "on productionwork.ProductionWorkOrdersID=productionworkorders.ProductionWorkOrdersID " +
            "inner join picking on productionworkorders.ProductionWorkOrdersNo=picking.ProductionWorkOrdersNo " +
            "inner join productmanage on productionworkorders.ProductsNo=productmanage.productManageNode " +
            "inner join department on productionwork.departmentId=department.departmentId " +
            "inner join productiontype on productionwork.ProductionTypeId=productiontype.ProductionTypeId " +
            "where ProductionWorkId=" + request.ProductionWorkId + "";
            var res = new ProductionDTO();
            list.Data = _productionRepository.GetAll(str, res, conn).ToList();
            return list;
        }
    }
}
